define([], function() {
    'use strict';

    /**
     * 分页的构造函数
     * @param {*} - options
     * @param id - 容器id
     * @param pageSize - 每页的数据条数
     * @param totalCount - 总数
     * @param pageNum - 当前页数
     * @param sizeSelect - 分页条数的数据
     * 
     * @function clickChange - 点击切换事件
     */
    function Pagination(o){
        if(!o.id){
            throw '分页容器id没有给定';
        }
        this.id = o.id;
        this.box = document.getElementById(o.id);
        this.box.className = 'yby-pagination-box';
        this.pageSize = o.pageSize || 10;
        this.totalCount = o.totalCount || 0;
        this.pageNum = o.pageNum || 0;
        if(typeof o.sizeSelect != "undefined" && !o.sizeSelect instanceof Array){
            throw '分页数据不正确';
        }
        this.sizeSelect = o.sizeSelect || null;
        this.clickChange = o.clickChange;
        this.sizeChange = o.sizeChange;
        this.init();
    }

    Pagination.prototype.init = function(){
        this.setPage();
    }

    Pagination.prototype.reset = function(option){
        if(option.pageNum){
            this.pageNum = parseInt(option.pageNum);
        }
        if(option.pageSize){
            this.pageSize = parseInt(option.pageSize);
        }
        if(option.totalCount){
            this.totalCount = option.totalCount;
        }
        this.setPage();
    }

    Pagination.prototype.setPage = function(){
        var that = this;
        var pageSize = this.pageSize;
        var pageNum = this.pageNum;
        var totalCount = this.totalCount;
        var totalPage = Math.ceil(totalCount / pageSize);
        totalPage = totalPage == "0" ? "1" : totalPage;
        var str = '<div class="pagination-box"><span class="page-item pageInfo">第'+ (pageNum + 1) +'页|共'+ totalPage +'页|共'+ totalCount +'条</span>'
        if(this.sizeSelect){
            str += '<select class="page-item pageSelect">'
            this.sizeSelect.forEach(function(item){
                str += '<option value="'+ item +'" '+ (item == that.pageSize ? "selected" : "") +'>'+ item +'条/页</option>';
            })
            str += '</select>';
        }
        str += '<span class="page-item prev '+ (pageNum > 0 ? "" : "disabled" ) +'">前一页</span>';
        if(totalPage <= 10){
            for(var i = 0;i < totalPage;i++){
                str += '<span class="page-item '+ (pageNum == i ? "current" : "") +'">'+ (i + 1) +'</span>';
            }
        }else{
            if(pageNum < 4){
                var endNum = pageNum + 3 <= 5 ? 5 : pageNum + 3; 
                for(var i = 0;i < endNum;i++){
                    str += '<span class="page-item '+ (pageNum == i ? "current" : "") +'">'+ (i + 1) +'</span>';
                }
                str += '<span class="page-item more">...</span>';
                str += '<span class="page-item">'+ totalPage +'</span>';
            }else if(pageNum > totalPage - 5){
                var startNum = pageNum - 1 > totalPage - 5 ? totalPage - 4 : pageNum - 1;
                str += '<span class="page-item">1</span><span class="more">...</span>';
                for(var i = startNum - 1;i < totalPage;i++){
                    str += '<span class="page-item '+ (pageNum == i ? "current" : "") +'">'+ (i + 1) +'</span>';
                }
            }else{
                str += '<span class="page-item">1</span><span class="page-item more">...</span>';
                for(var i = pageNum - 2;i <= pageNum + 2;i++){
                    str += '<span class="page-item '+ (pageNum == i ? "current" : "") +'">'+ (i + 1) +'</span>';
                }
                str += '<span class="page-item more">...</span>';
                str += '<span class="page-item">'+ totalPage +'</span>';
            }
        }
        str += '<span class="page-item next '+ (pageNum < totalPage - 1 ? "" : "disabled" ) +'">后一页</span></div>';
        this.box.innerHTML = str;

        var pages = this.box.getElementsByClassName('page-item');
        for(var i = 0;i < pages.length;i++){
            pages[i].addEventListener('click', function(e){
                var _this = e.target;
                var tagClass = _this.className;
                console.log(tagClass);
                var type = "", nextNum;
                if(/(pageInfo|pageSelect|more)/.test(tagClass)){
                    return;
                }
                if(/prev/.test(tagClass)){
                    type = 'prev';
                    nextNum = pageNum - 1;
                }else if(/next/.test(tagClass)){
                    type = 'next';
                    nextNum = pageNum + 1;
                }else if(/current/.test(tagClass)){
                    type = 'current';
                    nextNum = pageNum;
                }else{
                    type = 'page';
                    nextNum = parseInt(_this.innerText) - 1;
                }
                console.log(type, nextNum);
                if(nextNum < 0 || nextNum > totalPage - 1 || nextNum == pageNum){
                    return;
                }
                that.reset({pageNum:nextNum+''});
                if(that.clickChange){
                    that.clickChange(nextNum, that.pageSize);
                }
            })
        }
        if(this.box.getElementsByClassName("pageSelect").length > 0){
            this.box.getElementsByClassName("pageSelect")[0].addEventListener('change', function(e){
                var _this = e.target;
                var value = _this.value;
                if(that.sizeChange){
                    that.sizeChange(that.pageNum, value);
                }
            })
        }
    }

    return Pagination;
});